package com.xzx.car_accessories.web;

import com.alibaba.fastjson.JSON;
import com.xzx.car_accessories.domain.CarAccessory;
import com.xzx.car_accessories.domain.Inventory;
import com.xzx.car_accessories.service.CarAccessoriesManageService;
import com.xzx.car_accessories.service.InventoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

/**
 * @author: geeshaw
 * @date: 2021-10-17 11:05:30
 * @email: shaw@bupt.edu.cn
 * @desc: 汽车库存信息管理
 **/

@RestController
@RequestMapping(value = "/inventory")     // 通过这里配置使下面的映射都在/inventory
public class InventoryController {

    // 创建线程安全的Map，模拟汽车配件信息的存储
    static Map<Long, Inventory> carInventories= Collections.synchronizedMap(new HashMap<Long, Inventory>());

    @Autowired
    private InventoryService inventoryService;

    /**
     * 处理的 GET 请求，查询汽车配件的分页信息
     */
    @GetMapping("/findPage")
    @ApiOperation("查询汽车配件的分页信息")
    public String get_inventory_page(String pageNum, String pageSize, String searchWord)
    {
        System.out.println(searchWord);
        System.out.println(pageNum);
        System.out.println(pageSize);
        List<Inventory> contentTotal = inventoryService.search_accessory(searchWord);
        List<Inventory> content = inventoryService.search_findpage_accessory(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize), searchWord);
        Map<String, Object> findPageData = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();

        int totalSize = contentTotal.size();
        int totalPages;

        if(totalSize % Integer.parseInt(pageSize) == 0)
        {
            totalPages =  totalSize / Integer.parseInt(pageSize);
        }
        else
        {
            totalPages =  totalSize / Integer.parseInt(pageSize) + 1;
        }

        data.put("pageNum", pageNum);
        data.put("pageSize", pageSize);
        data.put("totalSize", totalSize);
        data.put("totalPages", totalPages);
        data.put("content", content);
        findPageData.put("msg", null);
        findPageData.put("code", 200);
        findPageData.put("data", data);

        // 封装成功
        System.out.println(JSON.toJSONString(findPageData));
        return JSON.toJSONString(findPageData);
    }


    @PostMapping("/in")
    @ApiOperation("入库")
    public String inventory_inbound(@RequestBody Inventory inventory)
    {
        System.out.println(inventory);
        int i = inventoryService.inbound(inventory.getCid(), inventory.getNum_bound());
        Map<String, Integer> res = new HashMap<String,Integer>();
        if(i>0){
            res.put("code", 200);
        }
        else{
            res.put("code", 500);
        }

        return JSON.toJSONString(res);
    }

    @PostMapping("/out")
    @ApiOperation("出库")
    public String inventory_outbound(@RequestBody Inventory inventory)
    {
        System.out.println(inventory);
        int i = inventoryService.outbound(inventory.getCid(), inventory.getNum_bound());
        Map<String, Integer> res = new HashMap<String,Integer>();
        if(i>0){
            res.put("code", 200);
        }
        else{
            res.put("code", 500);
        }

        return JSON.toJSONString(res);
    }

    @GetMapping("/staticinfo")
    @ApiOperation("出库")
    public String staticinfo()
    {
        return JSON.toJSONString(inventoryService.staticInfo());
    }
}
